home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #23 (Aug 87) / find traps source / IsTrap.c
C/C++ Source or Header  |  1987-06-22  |  3KB  |  127 lines

  1. /* IsTrap.c: Detect trap availability
  2.  
  3.    Written by Joel West in MPW C, June 1987
  4.  
  5.    Compiled as an MPW tool (shell command); usage:
  6.        IsTrap A010 A060    print information on traps $A010 and $A060
  7.     IsTrap A800 -A830    information on all traps from $A800 to $A830
  8.  
  9.    There is one option letter (which like UNIX and unlike MPW, must come first):
  10.        -p    show detailed "progress" information
  11.  
  12.    Status values returned:
  13.        0    ok
  14.     1    syntax error
  15.     2    trap not implemented
  16. */
  17.  
  18. #include <Memory.h>            /* for THz heap zone pointer */
  19. #include <OSUtils.h>            /* for GetTrapAddress() */
  20. #include <stdio.h>
  21. #define UNDEFTRAP 0x9F
  22. #define GETLONG(addr) *( (long *) addr)    /* grab a low-memory global value */
  23. #define ROMBase    GETLONG(0x2AEL)
  24. typedef short Half;
  25. typedef unsigned short UHalf;        /* traps are Axxx, normally negative numbers */
  26. typedef unsigned long Addr;        /* for unsigned address comparisons */
  27.  
  28. int strspn(),strlen();
  29. long MyGetTrapAddr();
  30. void syntaxerr();
  31.  
  32. THz syshz;
  33. #define INSYSHEAP(a) (a > (long) syshz && a < (long) syshz->bkLim)
  34.  
  35. main(argc,argv)
  36. int argc;
  37. char **argv;
  38. {    int argno,len,status;
  39.     Boolean range,verbose=0;
  40.     Addr trapword, trapaddr, unimpaddr, rombegin;
  41.     UHalf oldword=0,t;
  42.     char *p;
  43.     
  44.     unimpaddr = GetTrapAddress(UNDEFTRAP);
  45.     rombegin = ROMBase;
  46.     syshz = SystemZone();
  47.  
  48.     argno = 1;                        /* parameter number */
  49.     if (argno < argc && ! strcmp(argv[argno], "-p"))    /* -v for you UNIX types */
  50.     {    verbose++;
  51.          argno++;
  52.     }
  53.  
  54.     if (argno >= argc)
  55.         syntaxerr(argv);
  56.  
  57.     if (verbose)
  58.     {   printf("ROM @ %X\n", rombegin);
  59.         printf("System heap from %X to %X\n", syshz, syshz->bkLim);
  60.     }
  61.  
  62.     status = 0;
  63.     for (; argno<argc; argno++)
  64.     {   range = 0;
  65.         p = argv[argno];
  66.         if (*p == '-')
  67.         {   p++;
  68.         range++;
  69.         }
  70.         len = strlen(p);
  71.         if (len && len == strspn(p, "0123456789ABCDEF"))
  72.         {   sscanf(p,"%lx",&trapword);
  73.         if (range)
  74.             t = oldword+1;
  75.         else
  76.             t = trapword;
  77.         for (; t<=trapword; t++)
  78.         {   trapaddr = MyGetTrapAddr(t);
  79.             printf("Trap %lX is ", t);
  80.             if (verbose)
  81.             printf("%lX, ", trapaddr);
  82.             if (trapaddr == unimpaddr)
  83.             {   printf("undefined\n");
  84.             status = 2;            /* indicate result to shell */
  85.             }
  86.             else 
  87.             if (trapaddr >= rombegin)
  88.                 printf("in ROM\n");
  89.             else if (INSYSHEAP(trapaddr))
  90.                 printf("patched\n");
  91.             else    /* in application heap? */
  92.                 printf("overridden\n");
  93.         }
  94.         oldword = trapword;
  95.         }
  96.         else
  97.         syntaxerr(argv);
  98.     }
  99.     exit(status);
  100. }
  101.  
  102. void syntaxerr(argv)
  103. char **argv;
  104. {    fprintf(stderr, "# %s - invalid syntax.\n", argv[0]);
  105.     fprintf(stderr, "# %s - usage:  %s [-p] trap…\n", argv[0], argv[0]);
  106.     exit(1);
  107. }
  108. /* Find the trap address for a given trap word.
  109.    The 128K ROM provides NGetTrapAddress (a glue routine), which
  110.    distinguishes between OS and Toolbox traps.  This eventually
  111.    uses the same trap number as GetTrapAddress, so it seems
  112.    to work fine on the 64K ROM.
  113. */
  114. long MyGetTrapAddr(trapword)
  115. UHalf trapword;
  116. {    UHalf trapnum;
  117.     TrapType typ;
  118.     if ((long)trapword < 0x0000A800L)
  119.     {   typ = OSTrap;
  120.         trapnum = trapword & 0xFF;
  121.     }
  122.     else
  123.     {   typ = ToolTrap;
  124.         trapnum = trapword & 0x3FF;
  125.     }
  126.     return NGetTrapAddress(trapnum, typ);
  127. }